package ua.kharkov.knure.dereza.summarytask4.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.dereza.summarytask4.core.Constants;
import ua.kharkov.knure.dereza.summarytask4.core.PasswordEncryptor;
import ua.kharkov.knure.dereza.summarytask4.db.bean.ClientBean;
import ua.kharkov.knure.dereza.summarytask4.db.dao.ClientDAO;
import ua.kharkov.knure.dereza.summarytask4.db.dao.DAOFactory;
import ua.kharkov.knure.dereza.summarytask4.db.dao.LocaleDAO;
import ua.kharkov.knure.dereza.summarytask4.db.dao.mysql.MysqlDAOFactory;
import ua.kharkov.knure.dereza.summarytask4.db.dto.ClientDTO;
import ua.kharkov.knure.dereza.summarytask4.db.dto.LocaleDTO;

/**
 * Command to save the settings changes.
 * 
 * @author dereza
 * 
 */
public class ApplySettingsCommand extends Command {

	private static final long serialVersionUID = 8965130169508160425L;

	private static final Logger log = Logger
			.getLogger(ApplySettingsCommand.class);

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res) {
		log.debug("Command starts");

		// gets client's session
		HttpSession session = req.getSession(false);

		// gets ClientBean from session
		ClientBean clientBean = (ClientBean) session.getAttribute("clientbean");
		log.trace("Got clientbean  from session --> " + clientBean);

		// if clientBean == null forward to login page
		if (clientBean == null) {
			String forward = Constants.COMMAND_LOGOUT;
			return forward;
		}

		// gets changed information from request
		String name = req.getParameter("name");
		String surName = req.getParameter("surName");
		String mobileNumber = req.getParameter("mobileNumber");
		String locale = req.getParameter("locale");
		String password = req.getParameter("password");

		int clientId = clientBean.getId();
		String role = clientBean.getRole();

		// gets md5-hash code of password
		PasswordEncryptor encryptor = new PasswordEncryptor();
		password = encryptor.encode(password);

		// creates the required DAO generator
		MysqlDAOFactory mysqlFactory = (MysqlDAOFactory) DAOFactory
				.getDAOFactory(DAOFactory.MYSQL);

		// creates DAO for client
		ClientDAO clientDAO = mysqlFactory.getClientDAO();
		ClientDTO clientDTO = clientDAO.findClientById(clientId);
		log.trace("Found client in db --> " + clientDTO);

		// error handler
		String errorMessage = null;
		String forward = Constants.PAGE_ERROR_PAGE;
		if (!clientDTO.getPassword().equals(password)) {
			errorMessage = "Wrong password";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}

		// prepares information for update
		clientDTO.setName(name);
		clientDTO.setSurName(surName);
		clientDTO.setMobileNumber(mobileNumber);
		LocaleDAO localeDAO = mysqlFactory.getLocaleDAO();
		LocaleDTO localeDTO = localeDAO.findLocaleByName(locale);
		int localeId = localeDTO.getId();
		clientDTO.setLocale(localeId);
		log.trace("Client after changes --> " + clientDTO);

		// updates information
		try {
			clientDAO.updateClient(clientDTO);
		} catch (Error e) {
			errorMessage = "Cannot execute operation";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}
		log.info(clientBean.getLogin() + " --> changed own settings.");

		if (role.equals(Constants.ADMIN))
			forward = Constants.COMMAND_ADMIN_LIST_CARDS;
		if (role.equals(Constants.CLIENT))
			forward = Constants.COMMAND_CLIENT_LIST_CARDS;

		log.debug("Command finished");
		return forward;
	}

	@Override
	public String toString() {
		return "ApplySettingsCommand";
	}
}
